package psn.hui.bus.controller;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import psn.hui.bus.domain.Check;
import psn.hui.bus.domain.Rent;
import psn.hui.bus.service.CheckService;
import psn.hui.bus.service.RentService;
import psn.hui.bus.vo.CheckVo;
import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.utils.DataGridView;
import psn.hui.sys.utils.JsonResult;

@RestController
@RequestMapping("check")
public class CheckController {
	
	@Autowired
	private CheckService checkService;
	@Autowired
	private RentService rentService;
	
	/**
	 * 	根据出租单号查询
	 * @return
	 */
	@RequestMapping("checkRentExist")
	public DataGridView checkRentExist(String rentid) {
		DataGridView dataGridView = new DataGridView();
		try {
			Rent rent = rentService.queryRentByRentId(rentid);
			if(null == rent) {
				//出租单不存在
				dataGridView.setCode(1);
				dataGridView.setMsg(SysConstant.RENT_EXISI_ERROR_NOTEXIST_TIPS);
			}else if(rent.getRentflag() == SysConstant.RENT_FLAG_RETURN) {
				//出租单是已归还
				dataGridView.setCode(1);
				dataGridView.setMsg(SysConstant.RENT_EXISI_ERROR_RETURNED_TIPS);
			}else {
				//未归还
				dataGridView.setCode(0);
				dataGridView.setData(rent);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataGridView;
	}
	/**
	 * 	根据出租单号，初始化检查单信息，获取车辆，出租单，客户信息
	 * @param rentid
	 * @return
	 */
	@RequestMapping("initCheckFormData")
	public Map<String,Object> initCheckFormData(String rentid){
		return checkService.initCheckFormData(rentid);
	}
	/**
	 * 	保存检查单信息
	 * @param check
	 * @return
	 */
	@RequestMapping("addCheck")
	public JsonResult addCheck(Check check) {
		try {
			//设置创建时间
			check.setCreatetime(new Date());
			if(checkService.addCheck(check) > 0) {
				return SysConstant.ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.ADD_ERROR;
	}
	/**
	 * 	模糊查询检查单列表
	 * @param checkVo
	 * @return
	 */
	@RequestMapping("checkList")
	public DataGridView checkList(CheckVo checkVo) {
		try {
			return checkService.queryAllCheck(checkVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new DataGridView();
	}
	/**
	 * 修改检查单信息
	 * @param check
	 * @return
	 */
	@RequestMapping("updateCheck")
	public JsonResult updateCheck(Check check) {
		try {
			if(checkService.updateCheck(check) > 0) {
				return SysConstant.UPDATE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.UPDATE_ERROR;
	}
	
}
